package com.bookmngsys.config;

import com.bookmngsys.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * @author han
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {

    @Bean
    LoginInterceptor localInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localInterceptor()).addPathPatterns("/api/**")
                .excludePathPatterns("/api/book/getBooks", "/api/user/login", "/api/user/register","/api/book/getBookDetail",
                        "/swagger-resources/**", "/webjars/**",
                        "/swagger-ui/**", "/v2/api-docs");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("映射");
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println(path);
        String dirPath = path.substring(1);
        System.out.println(dirPath);
        registry.addResourceHandler("/barcode/**").addResourceLocations("file:" + dirPath);
    }
}